/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.io;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.servlet.http.HttpSession;

/**
 * This class wrapps a {@link HttpSession} and allows to modify the
 * parameters of the session without modifying the underlying session.
 */
public class HttpSessionProxy
    extends HttpSessionWrapper
{

  /**
   * the attributes added to this session.
   */
  private transient final Map<String, Object> attributes = new TreeMap<String, Object>();

  /**
   * the attributes removed from this session.
   */
  private transient final Set<String> removedAttributes = new TreeSet<String>();



  /**
   * Returns the object bound with the specified name in this session, or
   * <code>null</code> if no object is bound under the name.
   * 
   * @param name
   *          a string specifying the name of the object
   * @return the object with the specified name
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
   */
  @Override
  public Object getAttribute(final String name)
      throws IllegalStateException
  {
    return ProcessProxy.getValue(name, attributes, removedAttributes, super.getAttribute(name));
  }



  /**
   * Returns an <code>Enumeration</code> of <code>String</code> objects
   * containing the names of all the objects bound to this session.
   * 
   * @return an <code>Enumeration</code> of <code>String</code> objects
   *         specifying the names of all the objects bound to this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#getAttributeNames()
   */
  @Override
  public Enumeration<String> getAttributeNames()
      throws IllegalStateException
  {
    return ProcessProxy.getNames(attributes, removedAttributes, super.getAttributeNames());
  }



  /**
   * As of Version 2.2, this method is replaced by {@link #getAttribute}.
   * 
   * @param name
   *          a string specifying the name of the object
   * @return the object with the specified name
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @deprecated
   * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
   */
  @Override
  @Deprecated
  public Object getValue(final String name)
      throws IllegalStateException
  {
    return getAttribute(name);
  }



  /**
   * As of Version 2.2, this method is replaced by {@link #getAttributeNames}.
   * 
   * @return an array of <code>String</code> objects specifying the names of
   *         all the objects bound to this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @deprecated
   * @see javax.servlet.http.HttpSession#getValueNames()
   */
  @Override
  @Deprecated
  public String[] getValueNames()
      throws IllegalStateException
  {
    final List<String> attributesName = new ArrayList<String>();
    for (final Enumeration<String> nameEnumeration = getAttributeNames(); nameEnumeration.hasMoreElements();) {
      attributesName.add(nameEnumeration.nextElement());
    }
    return attributesName.toArray(new String[attributesName.size()]);
  }



  /**
   * As of Version 2.2, this method is replaced by {@link #setAttribute}.
   * 
   * @param name
   *          the name to which the object is bound; cannot be null
   * @param value
   *          the object to be bound; cannot be null
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @deprecated
   * @see javax.servlet.http.HttpSession#putValue(java.lang.String,
   *      java.lang.Object)
   */
  @Override
  @Deprecated
  public void putValue(final String name, final Object value)
      throws IllegalStateException
  {
    setAttribute(name, value);
  }



  /**
   * Removes the object bound with the specified name from this session. If the
   * session does not have an object bound with the specified name, this method
   * does nothing.
   * <p>
   * After this method executes, and if the object implements
   * <code>HttpSessionBindingListener</code>, the container calls
   * <code>HttpSessionBindingListener.valueUnbound</code>. The container then
   * notifies any <code>HttpSessionAttributeListener</code>s in the web
   * application.
   * 
   * @param name
   *          the name of the object to remove from this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
   */
  @Override
  public void removeAttribute(final String name)
      throws IllegalStateException
  {
    removedAttributes.add(name);
    attributes.remove(name);
  }



  /**
   * As of Version 2.2, this method is replaced by {@link #removeAttribute}.
   * 
   * @param name
   *          the name of the object to remove from this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @deprecated
   * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
   */
  @Override
  @Deprecated
  public void removeValue(final String name)
      throws IllegalStateException
  {
    removeAttribute(name);
  }



  /**
   * Binds an object to this session, using the name specified. If an object of
   * the same name is already bound to the session, the object is replaced.
   * <p>
   * After this method executes, and if the new object implements
   * <code>HttpSessionBindingListener</code>, the container calls
   * <code>HttpSessionBindingListener.valueBound</code>. The container then
   * notifies any <code>HttpSessionAttributeListener</code>s in the web
   * application.
   * <p>
   * If an object was already bound to this session of this name that implements
   * <code>HttpSessionBindingListener</code>, its
   * <code>HttpSessionBindingListener.valueUnbound</code> method is called.
   * <p>
   * If the value passed in is null, this has the same effect as calling
   * <code>removeAttribute()</code>.
   * 
   * @param name
   *          the name to which the object is bound; cannot be null
   * @param value
   *          the object to be bound
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String,
   *      java.lang.Object)
   */
  @Override
  public void setAttribute(final String name, final Object value)
      throws IllegalStateException
  {
    removedAttributes.remove(name);
    attributes.put(name, value);
  }



  /**
   * constructor.
   * 
   * @param session
   *          the wrapped session
   */
  public HttpSessionProxy(final HttpSession session)
  {
    super(session);
  }

}
